package com.redroid.findme;

//import com.redroid.findme.AddSnListAdapter.AddSnItemHandlerHolder;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
//import android.widget.ListView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddSnAct extends ListActivity {

	private String _choosensn;
	private String _login;
	private String _passwd;
	//private Contact contact = new Contact();
	
	//ListView listView1;
	
	@Override
	protected void onCreate(Bundle addsnstate) {
		// TODO Auto-generated method stub
		super.onCreate(addsnstate);
		//setContentView(R.layout.addsnlayout);
		/*
		 * Parte obsoleta
		 * 
		AddSnActItemHandler item_data[] = new AddSnActItemHandler[]
		{
				new AddSnActItemHandler(R.drawable.facebook, " Facebook"),
				new AddSnActItemHandler(R.drawable.twitter, " Twitter"),
				new AddSnActItemHandler(R.drawable.gplus, " Google+"),
				new AddSnActItemHandler(R.drawable.linkedin, " LinkedIn"),
				new AddSnActItemHandler(R.drawable.skype, " Skype"),
				new AddSnActItemHandler(R.drawable.instagram, " Instagram"),
				new AddSnActItemHandler(R.drawable.tumblr, " Tumblr")				
		};
		AddSnListAdapter adapter = new AddSnListAdapter(this, R.layout.addsnlayoutitem, item_data);
		listView1 = (ListView)findViewById(R.id.listaddsn);
		listView1.setAdapter(adapter);
		*/
		
		String[] socialnetworks = {"facebook", "twitter", "google+", "whatsapp",  
				"linkedIn", "skype", "tumblr" };
		
		ArrayAdapter<String> menuadapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_list_item_1, socialnetworks);
		setListAdapter(menuadapter);
	}
	
	   @Override
	    protected void onListItemClick(ListView l, View v, int position, long id){
	    	// TODO Auto-generated method stub
	    	super.onListItemClick(l, v, position, id);
	    	
	    	String item = (String)getListAdapter().getItem(position);
	    	setChoosensn((item.toLowerCase()));
	    	Toast.makeText(this, item.toLowerCase(),Toast.LENGTH_SHORT).show();
	    	//setChoosensn(item.toLowerCase());
	    	
	    	if (item.equalsIgnoreCase("facebook")) {
	    		DetailPopup("Facebook");
	    		
			} else if (item.equalsIgnoreCase("twitter")) {
	    		DetailPopup("Twitter");
	    		
			} else if (item.equalsIgnoreCase("google+")) {
	    		DetailPopup("Google+");
	    		
			} else if (item.equalsIgnoreCase("linkedin")) {
	    		DetailPopup("LinkedIn");
	    		
			} else if (item.equalsIgnoreCase("skype")) {
	    		DetailPopup("Skype");
	    		
			} else if (item.equalsIgnoreCase("tumblr")) {
	    		DetailPopup("Tumblr");
			} else if (item.equalsIgnoreCase("whatsapp")) {
	    		DetailPopup("WhatsApp");
			}
	   }

	   /*
	    * Popup de teste de armazenamento de detalhes
	    */
	   
	   public void testeContactAdc(){
		   Toast.makeText(this, 
				   "Social Network: " + getChoosensn() +
				   "\nUsername: " + getLogin() +
				   "\nPassword: " + getPassword() ,Toast.LENGTH_LONG).show();
	   }
	   
	   public void testAdc(){
		   if (this._login != null && this._passwd != null) {
			   DbHandler db = new DbHandler(this);
			   Log.d("Inserting: ","Inserting account...");
			   db.addAccount(new Contact(this._login, this._passwd,
					   this._choosensn));
			   
			   Log.d("Reading: ", "Reading all contacts:");
			   List<Contact> allcontacts = db.getAllContacts();
			   
			   for(Contact cn : allcontacts){
		   String log = "Id: " + cn.getID() + " Username: " + cn.getUsername() +
		   " Password: " + cn.getPassword() + " Social Network: " + cn.getSN();
		   Log.d("Name: ", log);
		   //Toast.makeText(this, log, Toast.LENGTH_LONG).show();
			   }
			   
		   }
	   }
	   
	   /*
	    * Popup para input de detalhes
	    * @param socialnetwork string
	    */
	   public void DetailPopup(String socialnetwork){
		   LayoutInflater linf = LayoutInflater.from(this);
		   View popup = linf.inflate(R.layout.accinputdialogxml, null);

		   	AlertDialog.Builder adbuilder = new AlertDialog.Builder(this);		   
		   	adbuilder.setView(popup);

		   	final EditText usernameinput = (EditText)popup.findViewById(R.id.usernameinput);
		   	final EditText passwordinput = (EditText)popup.findViewById(R.id.passwordinput);
		   	final TextView snlabelpopup = (TextView)popup.findViewById(R.id.snlabelpopup);
		   	snlabelpopup.setText(socialnetwork, null);
		   	
		   	adbuilder.setCancelable(false).setPositiveButton("OK", 
		   			new DialogInterface.OnClickListener() {
		   			public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
		   			setLogin((usernameinput.getText().toString()));	
		   			//setLogin(usernameinput.getText().toString());
		   			setPassword((passwordinput.getText().toString()));
					//setPassword(passwordinput.getText().toString());
					testAdc();
		   			//testeContactAdc();
				}
			}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});

		   	AlertDialog alrtdialog = adbuilder.create();
		   	alrtdialog.show();
	   }
	   
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	/*
	 * Setters and Getters
	 */
	
	public void setChoosensn(String choosensn){
		this._choosensn = choosensn;
	}
	
	public String getChoosensn(){
		return this._choosensn;
	}
	
	public void setLogin(String login){
		this._login = login;
	}
	
	public String getLogin(){
		return this._login;
	}
	public void setPassword(String password){
		this._passwd = password;
	}
	
	public String getPassword(){
		return this._passwd;
	}	
}
